package com.zpark.controller;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.DownloadUrl;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * @PackageName: com.zpark.controller
 * @ClassName: QiNiuController
 * @Description:
 * @author: RZS
 * @date: 2024/5/14  10:48
 */
@RestController
public class QiNiuController {

    // accessKey  公钥
    public static String accessKey = "FBFdt3OsvH1osLByc2IZUNlD6MAztUWZAD51Gyjb";

    // secretKey  密钥
    public static String secretKey = "i0OkDFKbxraNQ_Mx8WwWjXCI8CTnxfifCJqIo-h_";

    // 存储空间命名
    public static String bucket = "jushanpay-images";

    // 定义CDN域名
    public static String domain = "sd6351s6l.hn-bkt.clouddn.com/";

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file){

        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        //...其他参数参考类注释

        UploadManager uploadManager = new UploadManager(cfg);
        String substring = file.getOriginalFilename().
                substring(file.getOriginalFilename().lastIndexOf("."));

        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = UUID.randomUUID().toString() + substring;  // key就是文件名字

        try {
            // 获取当前file对象的字节数组
            byte[] uploadBytes = file.getBytes();
            // 创建上传凭证
            Auth auth = Auth.create(accessKey, secretKey);
            // 指定上传的存储空间名字
            String upToken = auth.uploadToken(bucket);

            try {
                // 将文件上传到七牛云
                Response response = uploadManager.put(uploadBytes, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
            } catch (QiniuException ex) {
                ex.printStackTrace();
                if (ex.response != null) {
                    System.err.println(ex.response);

                    try {
                        String body = ex.response.toString();
                        System.err.println(body);
                    } catch (Exception ignored) {
                    }
                }
            }
        } catch (Exception ex) {
            //ignore
        }


        return null;
    }

    @PostMapping("/download")
    public String download() throws QiniuException {
        // useHttps 是否使用 https【必须】
        // key      下载资源在七牛云存储的 key【必须】
        DownloadUrl url = new DownloadUrl(domain, true, "0bc872ee-260b-42fd-8b62-fff77753e572.jpg");
        url.setAttname("hh") // 配置 attname
                .setFop("") // 配置 fop
                .setStyle("style", "", ""); // 配置 style
        String urlString = url.buildURL();
        System.out.println(urlString);

        return null;
    }
}
